package com.mew.chess.core.common.handler;

import com.mew.chess.core.common.provide.GameFeignService;
import com.mew.common.dc.redis.AucRedisService;
import com.mew.common.enm.ExitType;
import com.mew.common.exception.ResponseCode;
import com.mew.common.rest.bo.GameSession;
import com.mew.common.rest.bo.MewSession;
import com.mew.common.rest.game.ChessEnterRoomRequest;
import com.mew.common.rest.game.ChessEnterRoomResponse;
import com.zebra.common.OfflineType;
import com.zebra.common.OnlineType;
import com.zebra.remoting.config.EmptyMessage;
import com.zebra.remoting.connection.Connection;
import com.zebra.remoting.protocol.ZebraCommand;
import com.zebra.remoting.tms.TmsConfigInfoManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service
@Slf4j
public class EnterExitRoomService {

    @Resource
    private GameFeignService gameFeignService;
    @Autowired
    private ChessSessionManager chessSessionManager;
    @Autowired
    private AucRedisService aucRedisService;


    public ChessEnterRoomResponse doFeignEnterRoom(GameSession session, ZebraCommand command, int roomId,  int type){

        Connection connection = Connection.getByChannel(command.getChannel());
        int gwId = null == connection ? -9 : connection.getServerId();
        ChessEnterRoomRequest request = ChessEnterRoomRequest.builder()
                .uid(session.getUserId())
                .gameId(command.getGameId())
                .roomId(roomId)
                .tableId(session.getTableId())
                .seatNo(session.getSeatNo())
                .type(type)
                .serverId(TmsConfigInfoManager.getInstance().getServerInfo().getServerId())
                .gatewaySid(gwId)
                .build();

        ChessEnterRoomResponse roomResponse = gameFeignService.enterRoom(request);
        if (ResponseCode.Success.code() != roomResponse.getErrorState()){
            return roomResponse;
        }
        return roomResponse;
    }


//----------exit---------------------
    public void exitRoom(long userId, int exitCode){
        gameFeignService.exitRoom(userId, exitCode);
    }


    public void offlineByGw(ZebraCommand<EmptyMessage> command, MewSession session, GameSession chessSession){
        long userId = command.getUserId();
        if (null == chessSession){
            return;
        }

        if (null == session || chessSession.getTk() != session.tk){
            chessSessionManager.remove(chessSession);
            return;
        }

        if (command.getToken() != session.tk){
            return;
        }

        if (OfflineType.InactiveDiff.code() == command.getRequest().getMsg()
                || (OfflineType.Push.code() == command.getRequest().getMsg() && OnlineType.Online.value() == session.onl)){
            return;
        }

        if (OfflineType.Expired.code() == command.getRequest().getMsg()){
            exitRoom(userId, ExitType.Expires.code());
        } else if (OfflineType.Repeat.code() != command.getRequest().getMsg()){
            aucRedisService.offlineSession(userId, chessSession.getTk());
        }

        chessSessionManager.remove(chessSession);
    }

    public void offline(GameSession chessSession){
        if (null == chessSession){
            return;
        }
        chessSessionManager.remove(chessSession);
        aucRedisService.offlineSession(chessSession.getUserId(), chessSession.getTk());
    }


}
